﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Controls
{
    public partial class BoxManagerPostNews : System.Web.UI.UserControl
    {
        public DataTable dt;
        public LIB.Validate saveimage;
        public int rdb;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                rbtCanBan.Checked = true;
                //Tinh Thanh Pho
                dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_TinhThanhPho");
                ListItem lt = new ListItem();
                lt = new ListItem("Chọn thành phố", "-1");
                ddlThanhPho.Items.Add(lt);
                lt = new ListItem("Chọn quận huyện", "-1");
                ddlQuanHuyen.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["TinhThanhPho_Ten"].ToString(), dt.Rows[i]["TinhThanhPho_ID"].ToString());
                    ddlThanhPho.Items.Add(lt);
                }
                //Loai Dia Oc
                DataTable dtl = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Category", "Category_ID2", "6");
                ListItem lt2 = new ListItem();
                lt2 = new ListItem("Vui lòng chọn ... ", "-1");
                ddlDanhMuc.Items.Add(lt2);
                for (int i = 0; i < dtl.Rows.Count; i++)
                {
                    lt2 = new ListItem(dtl.Rows[i]["Category_Name"].ToString(), dtl.Rows[i]["Category_ID2"].ToString());
                    ddlDanhMuc.Items.Add(lt2);
                }

                // Insert
                captchaImage.ImageUrl = new CaptchaProvider().CreateCaptcha();
                txtSoNhaTenDuong.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtDienTich.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                cktTomTat.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtPrice.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtTieuDe.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                cktChiTiet.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtFullName.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtAddress.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtEmail.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtDienThoai.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtDiDong.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtCompany.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");

                rpt_dmti.DataSource = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "Select * from tbl_DanhMucTienIch", true);
                rpt_dmti.DataBind();

                //Don Vi
                DataTable dtd = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_DonVi");
                for (int i = 0; i < dtd.Rows.Count; i++)
                {
                    ListItem lt3 = new ListItem(dtd.Rows[i]["DonVi_Name"].ToString(), dtd.Rows[i]["DonVi_ID"].ToString());
                    ddlDonVi.Items.Add(lt3);
                }


            }
        }

        protected void ddlThanhPho_SelectedIndexChanged(object sender, EventArgs e)
        {
            int tttid = Convert.ToInt32(ddlThanhPho.SelectedValue);
            dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_QuanHuyen", "TinhThanhPho_ID", tttid.ToString());
            ListItem lt = new ListItem();
            ddlQuanHuyen.Items.Clear();
            lt = new ListItem("Chọn quận huyện", "-1");
            ddlQuanHuyen.Items.Add(lt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lt = new ListItem(dt.Rows[i]["QuanHuyen_Ten"].ToString(), dt.Rows[i]["QuanHuyen_ID"].ToString());
                ddlQuanHuyen.Items.Add(lt);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            CaptchaProvider captchaPro = new CaptchaProvider();
            if (captchaPro.IsValidCode(txtCaptcha.Text))
            {
                int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_SanGiaoDich", "SanGiaoDich_ID")) + 1;

                if (rbtCanBan.Checked == true)
                {
                    rdb = 2;
                }
                if (rbtCanMua.Checked == true)
                {
                    rdb = 3;
                }
                if (rbtCanThue.Checked == true)
                {
                    rdb = 4;
                }
                if (rbtChoThue.Checked == true)
                {
                    rdb = 5;
                }
                int idtinhthanh = Convert.ToInt32(ddlThanhPho.SelectedValue);
                int idquanhuyen = Convert.ToInt32(ddlQuanHuyen.SelectedValue);
                int iddanhmuc = Convert.ToInt32(ddlDanhMuc.SelectedValue);
                double dientich = Convert.ToDouble(txtDienTich.Text);
                string cautruc = Server.HtmlEncode(cktTomTat.Text);
                int donvi = Convert.ToInt32(ddlDonVi.SelectedValue);
                int idemployee = Convert.ToInt32(Session["Employee_ID"].ToString());
                if (ddlThanhPho.SelectedIndex > 0)
                {
                    if (ddlQuanHuyen.SelectedIndex > 0)
                    {
                        if (ddlDanhMuc.SelectedIndex > 0)
                        {
                            if (fulImageSGD.HasFile)
                            {
                                if (LIB.Validate.CheckFileType(fulImageSGD.FileName))
                                {
                                    saveimage = new LIB.Validate();
                                    string filePathImageSGD = "/Resources/Images/Images-Sangiaodich/" + nextid + Path.GetExtension(fulImageSGD.FileName) + "";
                                    saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Sangiaodich/"), nextid + Path.GetExtension(fulImageSGD.FileName), 220, fulImageSGD.PostedFile.InputStream);
                                    string value1 = "" + nextid + "," + idemployee + "," + idtinhthanh + "," + idquanhuyen + ",6,N'" + ddlDanhMuc.SelectedItem.Text.ToString() + "'," + rdb + "," + donvi + ",N'" + txtTieuDe.Text + "',N'" + txtTieuDe.Text + "',N'" + filePathImageSGD + "',N'" + filePathImageSGD + "'," + float.Parse(txtDienTich.Text) + ",N'" + txtSoNhaTenDuong.Text + "',N'" + Server.HtmlEncode(cktTomTat.Text) + "',N'" + Server.HtmlEncode(cktChiTiet.Text) + "'," + float.Parse(txtPrice.Text) + ",N'Liên hệ chi tiêt',N'http://www.thailong.info/',N'" + txtFullName.Text + "',N'" + txtAddress.Text + "',N'" + txtEmail.Text + "',N'" + txtDienThoai.Text + "',N'" + txtDiDong.Text + "',N'" + txtCompany.Text + "','" + DateTime.Now.ToShortDateString() + "'";
                                    List<string> values1 = new List<string>();
                                    values1.Add(value1);
                                    DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_SanGiaoDich", "insertbyrows", "*", values1, false);
                                    values1.Clear();

                                    int check = 0;
                                    DataTable dt2 = DAL.DCollector.DBCollector.Instance.StoredSelectAllTable("tbl_DanhMucTienIch");
                                    for (int i = 0; i < dt2.Rows.Count; i++)
                                    {
                                        CheckBox checkboxdmti = (CheckBox)rpt_dmti.Items[i].FindControl("cbxdmti");
                                        if (checkboxdmti.Checked == true)
                                        {
                                            check = 1;

                                            int idtientich = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_TienIch", "TienIch_ID")) + 1;
                                            string value2 = "" + idtientich + "," + nextid + "," + Convert.ToInt32(dt2.Rows[i]["DanhMucTienIch_ID"].ToString()) + "";
                                            List<string> values2 = new List<string>();
                                            values2.Add(value2);
                                            DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_TienIch", "insertbyrows", "*", values2, false);
                                            values2.Clear();

                                        }
                                    }
                                    Response.Redirect("/home");
                                    if (check == 0)
                                    {
                                        int idtientich = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_TienIch", "TienIch_ID")) + 1;
                                        string value2 = "" + idtientich + "," + nextid + "," + Convert.ToInt32(dt2.Rows[0]["DanhMucTienIch_ID"].ToString()) + "";
                                        List<string> values2 = new List<string>();
                                        values2.Add(value2);
                                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_TienIch", "insertbyrows", "*", values2, false);
                                        values2.Clear();
                                        Response.Redirect("/home");
                                    }
                                }
                                else
                                {
                                    Response.Redirect("/dangtinmoi");
                                }
                            }
                            else
                            {
                                Response.Redirect("/dangtinmoi");
                            }
                        }
                        else
                        {
                            Response.Redirect("/dangtinmoi");
                        }
                    }
                    else
                    {
                        Response.Redirect("/dangtinmoi");
                    }
                }
            }
            else
            {
                Response.Redirect("/dangtinmoi");
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtSoNhaTenDuong.Text = "";
            txtDienTich.Text = "";
            cktTomTat.Text = "";
            txtPrice.Text = "";
            txtTieuDe.Text = "";
            cktChiTiet.Text = "";
            txtFullName.Text = "";
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtDienThoai.Text = "";
            txtDiDong.Text = "";
            txtCompany.Text = "";
        }
    }
}